package com.pearadmin.system.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.system.domain.SysAxis;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.system.service.ISysAxisService;

import java.util.List;

/**
 * 主轴监控Controller
 * 
 * @author Ryan
 * @date 2021-03-20
 */
@RestController
@RequestMapping("/system/axis")
public class SysAxisController extends BaseController
{
    private String prefix = "system/axis";

    @Autowired
    private ISysAxisService sysAxisService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/system/axis/main','system:axis:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询主轴监控列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/system/axis/data','system:axis:data')")
    public ResultTable list(@ModelAttribute SysAxis sysAxis, PageDomain pageDomain)
    {
        PageInfo<SysAxis> pageInfo = sysAxisService.selectSysAxisPage(sysAxis,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 条件查询主轴属性
     * @return
     */
    @ResponseBody
    @GetMapping("portionData")
    @PreAuthorize("hasPermission('/system/axis/portionData','system:axis:portionData')")
    public List<SysAxis> listDiv(SysAxis sysAxis){
        List<SysAxis> PortsysAxes = sysAxisService.selectPortionAxis(sysAxis);
        return PortsysAxes;
    }

    /**
     * 新增主轴监控
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/system/axis/add','system:axis:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存主轴监控
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/system/axis/add','system:axis:add')")
    public Result save(@RequestBody SysAxis sysAxis)
    {
        return decide(sysAxisService.insertSysAxis(sysAxis));
    }

    /**
     * 修改主轴监控
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/system/axis/edit','system:axis:edit')")
    public ModelAndView edit(Long axisId, ModelMap mmap)
    {
        SysAxis sysAxis = sysAxisService.selectSysAxisById(axisId);
        mmap.put("sysAxis", sysAxis);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 根据主轴id查询对应的模态框数据
     */
    @ResponseBody
    @GetMapping("/selectAxisInfo")
    @PreAuthorize("hasPermission('/system/axis/selectAxisInfo','system:axis:selectAxisInfo')")
    public SysAxis selectAxis(Long axisId){
         SysAxis sysAxisModel = sysAxisService.selectAxisInfo(axisId);
         return sysAxisModel;
    }

    /**
     * 修改保存主轴监控
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/system/axis/edit','system:axis:edit')")
    public Result update(@RequestBody SysAxis sysAxis)
    {
        return decide(sysAxisService.updateSysAxis(sysAxis));
    }

    /**
     * 删除主轴监控
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/system/axis/remove','system:axis:remove')")
    public Result batchRemove(String ids)
    {
        return decide(sysAxisService.deleteSysAxisByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{axisId}")
    @PreAuthorize("hasPermission('/system/axis/remove','system:axis:remove')")
    public Result remove(@PathVariable("axisId") Long axisId)
    {
        return decide(sysAxisService.deleteSysAxisById(axisId));
    }

    /**
     * 根据状态查询对应的条数
     */
    @ResponseBody
    @GetMapping("/selectAxisCountByState")
    @PreAuthorize("hasPermission('/system/axis/selectAxisCountByState','system:axis:selectAxisCountByState')")
    public int queryAxisCount(String axisState)
    {
        return sysAxisService.selectAxisCount(axisState);
    }

}
